/**
* All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
*
* MpmProjectBidDateHistoryService
* 项目立项变更-挂网开标日期
*
* 修改纪录
* 2022-01-26 版本：1.0 houyuhang 创建。
* @version 版本：1.0
* @author 作者：houyuhang
* 创建日期：2022-01-26
*/
package com.topscomm.mpm.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.mpm.pojo.MpmProjectBidDateEntity;
import com.topscomm.mpm.pojo.MpmProjectBidDateHistoryEntity;
import com.topscomm.mpm.pojo.MpmProjectChangeEntity;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.service.IMpmProjectService;
import com.topscomm.pub.util.StringBufferProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectBidDateHistoryService;
import com.topscomm.mpm.service.impl.auto.MpmProjectBidDateHistoryServiceAuto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "mpmProjectBidDateHistoryService")
@Scope(value = "singleton")
public class MpmProjectBidDateHistoryService extends MpmProjectBidDateHistoryServiceAuto implements IMpmProjectBidDateHistoryService {
    @Autowired
    private MpmProjectBidDateService mpmProjectBidDateService;
    @Autowired
    private MpmProjectService mpmProjectService;
    /**
     * @Title: removeAndAddBidDate
     * @description: 新增或更新挂网开标日期信息记录
     * @param
     * @return
     * @author: houyuhang
     * @date: 2022-01-21
     * @modify:
     */
    public void removeAndAddBidDate(Object object,long id) {
        if(id == 0) {
            return;
        }
        if(object == null) {
            return;
        }
        // 删除原挂网开标日期
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" {0} = '{1}' ", MpmProjectBidDateHistoryEntity.FieldProjecthistoryid, id);
        this.deleteByWhere(sql.toString());
        // 生成挂网开标日期
        sql.clear();
        sql.appendSingle("projectcode = (select {0} from {1} where id = '{2}' ) ", MpmProjectChangeEntity.FieldProjectcode2,
                MpmProjectChangeEntity.tableName, id);
        List<MpmProjectEntity> mpmProjectEntities = mpmProjectService.queryByWhere(sql.toString());
        if(mpmProjectEntities.size() == 0) {
            return;
        }
        long mpmProjectId = mpmProjectEntities.get(0).getId();
        List<Map<String,Object>> bidDateList = (List<Map<String,Object>>) object;
        List<BasicEntity> basicEntities = new ArrayList<>();
        for(Map<String,Object> bidDate : bidDateList) {
            MpmProjectBidDateHistoryEntity mpmProjectBidDateHistoryEntity = new MpmProjectBidDateHistoryEntity();
            mpmProjectBidDateHistoryEntity.convertFromMap(bidDate);
            mpmProjectBidDateHistoryEntity.setProjecthistoryid(id);
            mpmProjectBidDateHistoryEntity.setProjectid(mpmProjectId);
            basicEntities.add(mpmProjectBidDateHistoryEntity);
        }
        this.insertBatch(basicEntities);
    }
    /**
     * @Title: changeBidDate
     * @description: 立项变更结束后更新挂网开标日期
     * @param
     * @return
     * @author: houyuhang
     * @date: 2022-01-21
     * @modify:
     */
    public void changeBidDate(long historyid,long id) {
        if(id == 0) {
            return;
        }
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" projecthistoryid = '{0}' ",historyid);
        List<MpmProjectBidDateHistoryEntity> mpmProjectBidDateHistoryEntities = this.queryByWhere(sql.toString());
        List<BasicEntity> basicEntities = new ArrayList<>();
        // 删除原挂网开标日期
        sql.clear();
        sql.appendSingle(" {0} = '{1}' ", MpmProjectBidDateEntity.FieldProjectid, id);
        mpmProjectBidDateService.deleteByWhere(sql.toString());
        if(mpmProjectBidDateHistoryEntities.size() == 0) {
            return;
        }
        // 将立项变更通过的挂网开标日期信息记录同步到立项表中
        for (MpmProjectBidDateHistoryEntity mpmProjectBidDateHistoryEntity : mpmProjectBidDateHistoryEntities) {
            MpmProjectBidDateEntity mpmProjectBidDateEntity = new MpmProjectBidDateEntity();
            mpmProjectBidDateEntity.setProjectid(id);
            mpmProjectBidDateEntity.setBatchnumber(mpmProjectBidDateHistoryEntity.getBatchnumber());
            mpmProjectBidDateEntity.setProjectbidbegindate(mpmProjectBidDateHistoryEntity.getProjectbidbegindate());
            mpmProjectBidDateEntity.setProjectbidenddate(mpmProjectBidDateHistoryEntity.getProjectbidenddate());
            basicEntities.add(mpmProjectBidDateEntity);
        }
        mpmProjectBidDateService.insertBatch(basicEntities);
    }
    /**
     * @Title: removeAndAddBidDate
     * @description: 立项变更新增挂网开标日期信息记录
     * @param
     * @return
     * @author: houyuhang
     * @date: 2022-01-26
     * @modify:
     */
    public void addBidDateForChange(Object object,long projectid,long projecthistoryid) {
        if(projectid == 0 || projecthistoryid == 0) {
            return;
        }
        if(object == null) {
            return;
        }
        List<Map<String,Object>> bidDateList = (List<Map<String,Object>>) object;
        List<BasicEntity> basicEntities = new ArrayList<>();
        for(Map<String,Object> bidDate : bidDateList) {
            MpmProjectBidDateHistoryEntity mpmProjectBidDateHistoryEntity = new MpmProjectBidDateHistoryEntity();
            mpmProjectBidDateHistoryEntity.convertFromMap(bidDate);
            mpmProjectBidDateHistoryEntity.setProjectid(projectid);
            mpmProjectBidDateHistoryEntity.setProjecthistoryid(projecthistoryid);
            basicEntities.add(mpmProjectBidDateHistoryEntity);
        }
        this.insertBatch(basicEntities);
    }
}
